package com.lmk.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lmk.contants.StockCantants;
import com.lmk.domain.Product;
import com.lmk.domain.Stock;
import com.lmk.domain.StockLog;
import com.lmk.domain.StockWarningLog;
import com.lmk.dto.WarehousingDto;
import com.lmk.enums.GlobalErrorCode;
import com.lmk.exception.MyGlobalCustomException;
import com.lmk.feign.ProductService;
import com.lmk.mapper.StockLogMapper;
import com.lmk.mapper.StockMapper;
import com.lmk.mapper.StockWarningLogMapper;
import com.lmk.service.IStockService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.lmk.util.AssertUtil;
import com.lmk.util.CodeGenerateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kun
 * @since 2022-09-29
 */
@Service
public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements IStockService {
    @Autowired
    private ProductService productService;
    @Autowired
    private  StockMapper stockMapper;
    @Autowired
    private StockWarningLogMapper stockWarningLogMapper;
    @Autowired
    private StockCantants stockCantants;
    @Autowired
    private StockLogMapper stockLogMapper;

    /**
     * 入库
     * @param warehousingDto
     */
    @Override
    public void warehousing(WarehousingDto warehousingDto) {
        Product product = productService.getbyname(warehousingDto.getProduct_name());
        Stock stock = selectById(warehousingDto.getProduct_id());
        if (stock==null){
            Stock stock1 = new Stock();
            stock1.setCount(warehousingDto.getCount());
            stock1.setProductId(warehousingDto.getProduct_id());
            stock1.setProductName(warehousingDto.getProduct_name());
            stock1.setCreateTime(new Date());
            stock1.setTypeId(product.getTypeId());
            stockMapper.insert(stock1);
        }else{
            stock.setCount(stock.getCount()+warehousingDto.getCount());
            stock.setUpdateTime(new Date());
            stockMapper.updateById(stock);
        }

    }

    @Override
    public void issue(WarehousingDto warehousingDto) {
        Long product_id = warehousingDto.getProduct_id();
        String product_name = warehousingDto.getProduct_name();
        Integer count = warehousingDto.getCount();

        Stock stock = selectByName(product_name);

        int i = stock.getCount()-count;

        AssertUtil.isNotNull(stock,GlobalErrorCode.NOSTOCK_EXPIRED_ERROR);

        if (stock.getCount() - count < 1){
            throw new MyGlobalCustomException(GlobalErrorCode.STOCK_EXPIRED_ERROR);
        }
        stock.setCount(stock.getCount()-count);
        stockMapper.updateById(stock);
        StockLog log = new StockLog();
        log.setTypeId(stock.getTypeId());
        log.setProductId(stock.getProductId());
        log.setProductName(stock.getProductName());
        log.setCount(stock.getCount());
        log.setCreateTime(new Date());
        log.setStatus(0);
        stockLogMapper.insert(log);
        if (i < 10){
            StockWarningLog warningLog = new StockWarningLog();
            warningLog.setProductId(stock.getProductId());
            warningLog.setProductName(stock.getProductName());
            warningLog.setInfo(stock.getProductName()+"的库存不足，请及时补充");
            warningLog.setCurrentCount(10 -i);
            stockWarningLogMapper.insert(warningLog);

        }

    }


    private Stock selectByName(String product_name) {
        EntityWrapper<Stock> wrapper = new EntityWrapper<>();
        wrapper.eq("product_name",product_name);
        return selectOne(wrapper);
    }

    @Override
    public List<Stock> getlack() {
        return stockMapper.getlack();
    }
}
